﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4Part : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F4Part()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.F4Part.DoubleClick += F4Part_DoubleClick;
            this.F4Part.PopupMenuShowing += F4Part_PopupMenuShowing;
            this.f4PartContextMenu.ItemClicked += f4PartContextMenu_ItemClicked;
        }

        ////////////////////////////////
        /// Call Edit Form
        ////////////////////////////////
        private void callEditForm()
        {
            DataRow row = F4Part.GetFocusedDataRow();
            frm_Edit_F4Part editForm = new frm_Edit_F4Part();
            editForm.compid = row[0].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                LoadData();
            }
        }

        void f4PartContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4PartContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_F4Part add_Form = new frm_Add_F4Part();
                    add_Form.ShowDialog();
                    LoadData();
                    break;
                case "deleteSelection":
                    deleteRecord();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void F4Part_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4PartContextMenu.Show(MousePosition);
        }

        private void F4Part_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT * FROM [231_Comp] ORDER BY CompID";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF4Part.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("CompID", typeof(string));
                newTable.Columns.Add("CompName", typeof(string));
                newTable.Columns.Add("Product", typeof(string));
                newTable.Columns.Add("DCompName", typeof(string));
                newTable.Columns.Add("AssemblyTime", typeof(int));
                newTable.Columns.Add("InspectorTime", typeof(int));
                newTable.Columns.Add("WiringTime", typeof(int));
                newTable.Columns.Add("WCheckTime", typeof(int));
                newTable.Columns.Add("Serial", typeof(string));
                newTable.Columns.Add("Type", typeof(string));
                newTable.Columns.Add("CompGroup", typeof(string));
                newTable.Columns.Add("NormalSerial", typeof(string));
                newTable.Columns.Add("MACSerial", typeof(string));
                newTable.Columns.Add("Date", typeof(DateTime));
                newTable.Columns.Add("PersonCheck", typeof(string));
                gridF4Part.DataSource = newTable;
            }
            F4Part.Columns[0].Caption = "Comp ID";
            F4Part.Columns[1].Caption = "Comp Name";
            F4Part.Columns[2].Caption = "Product";
            F4Part.Columns[3].Caption = "DComp Name";
            F4Part.Columns[4].Caption = "Assembly Time";
            F4Part.Columns[5].Caption = "Inspector Time";
            F4Part.Columns[6].Caption = "Wiring Time";
            F4Part.Columns[7].Caption = "WCheck Time";
            F4Part.Columns[8].Caption = "Serial";
            F4Part.Columns[9].Caption = "Type";
            F4Part.Columns[10].Caption = "Comp Group";
            F4Part.Columns[11].Caption = "Normal Serial";
            F4Part.Columns[12].Caption = "MAC Serial";
            F4Part.Columns[13].Caption = "Date";
            F4Part.Columns[14].Caption = "Person Check";
            F4Part.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {            
            frm_Add_F4Part add_Form = new frm_Add_F4Part();
            add_Form.ShowDialog();
            LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        private void deleteRecord()
        {
            int[] rows = F4Part.GetSelectedRows();
            if (rows.Length <= 0)
            {
                MessageBox.Show("There is no selected record to delete");
            }
            else
            {
                if (MessageBox.Show("Are you really to delete selected records now?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string[] id_list = new string[rows.Length];
                    for (int i = 0; i < rows.Length; i++)
                    {
                        DataRow selected_row = F4Part.GetDataRow(rows[i]);
                        id_list[i] = "'" + selected_row[0].ToString() + "'";
                    }
                    string id = string.Join(",", id_list);

                    try
                    {
                        myConnection.Open();
                        string sql = "DELETE FROM [231_Comp] WHERE CompID IN (" + id + ")";
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                        cmd_sql.ExecuteNonQuery();
                        cmd_sql.Dispose();
                        myConnection.Close();
                        MessageBox.Show("F4 Part List deleted success.");
                        LoadData();
                    }
                    catch //(Exception ex)
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        //MessageBox.Show(ex.Message);
                        MessageBox.Show("Cannot delete F4 Part List. Please try again.");
                    }
                }
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            deleteRecord();
        }
    }
}
